package org.campuscontent.ui;

import java.awt.Component;
import java.awt.Frame;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JSplitPane;

import org.campuscontent.config.Config;
import org.campuscontent.main.MainFrame;
import org.campuscontent.model.AllocTable;
import org.campuscontent.util.GraphInspector;
import org.campuscontent.util.MovableEditor;

public abstract class TemplateBase extends JSplitPane implements ActionListener
{
	public MainFrame mf;
	
	public JButton submit;
	public JButton solution;
	
	public abstract String checkAns();
	public abstract int getId();
	
	public JPanel left;
	public JPanel right;
	
	public String sAnswer;
	public String paraConfig;

	protected GraphInspector graphInspector;
	
	public AllocTable alloc;
	public JScrollPane allocScrollPane;
	
	public TemplateBase(MainFrame mf)
	{
		this.mf = mf;

		submit = new JButton(Config.SUBMIT);
		solution = new JButton(Config.SOLUTION);
		submit.setBounds(5,10,90,30);
		solution.setBounds(5,45,90,30);
		submit.addActionListener(this);
		solution.addActionListener(this);
		
		left = new JPanel();
		left.setLayout(null);
		right = new JPanel();
		right.setLayout(null);
		right.add(submit);
		right.add(solution);
		
		this.setOrientation(JSplitPane.HORIZONTAL_SPLIT);
		
		this.add(right,JSplitPane.RIGHT);
		this.setDividerSize(Config.DIVIDER_SIZE);
		this.setBorder(null);
		this.setDividerLocation(Config.POSITION_TO_HIDE_PROBLEM_CONTROLS);
		this.setResizeWeight(1);
		this.add(left,JSplitPane.LEFT);
//		this.setPreferredSize(new Dimension(800,75));
		sAnswer = new String("solution");
		paraConfig = new String("paraconfig");
		
	}
	
	public String getParaConfig()
	{
		return paraConfig;
	}
	
	public void setParaConfig(String config)
	{
		paraConfig = config;
	}
	
	
	public boolean setPreview()
	{
		graphInspector = new GraphInspector( mf.ae.getGraph() );
		if(graphInspector.validate()!="valid"){
			JOptionPane.showMessageDialog(getWindowForComponent(mf.applet), graphInspector.validate());
			return false;
		}
		mf.rootPanel.setDividerLocation(Config.POSITION_TO_SHOW_TEMPLATE);
		this.setDividerLocation(Config.POSITION_TO_SHOW_PROBLEM_CONTROLS);
		return true;
	}
	
	public boolean exitPreview()
	{		
		mf.rootPanel.setDividerLocation(Config.ROOT_HEIGHT);
		this.setDividerLocation(Config.POSITION_TO_HIDE_PROBLEM_CONTROLS);
		return true;
	}
	
	public void initView()
	{		
	}
	
	public  void showSolution()
	{
		if (mf.isStudentView())
			submit.setEnabled(false);
	}

	public void actionPerformed(ActionEvent e)
	{
		String cmd = e.getActionCommand();

		if (cmd.equals(Config.SUBMIT))
		{
			submit();
		}

		if (cmd.equals(Config.SOLUTION))
		{
			showSolution();
			//submit.setEnabled(false);
		}
	}
	
	public void submit()
	{
		String str = checkAns();
		if (str.equals(""))
		{
			JOptionPane.showMessageDialog(getWindowForComponent(mf.applet), "Empty reply");
		}
		else
		{
			if (str.equals("True"))
				JOptionPane.showMessageDialog(getWindowForComponent(mf.applet), Config.rightAnswer_de);
			else
				JOptionPane.showMessageDialog(getWindowForComponent(mf.applet),	Config.wrongAnswer_de);				
		}
	}
	
	public boolean updateToLObjectNew()
	// if you need check the user whether have set the answers before save changes to server
	// you should add the statement in the child of templateBase;
	{
		if (mf.loidMain.equals("-1"))
		{
			mf.lobject.setLoid("-1");
			mf.lobject.setTitle(mf.problemCtrl.lobjectTitle.getText().trim());
			mf.lobject.setAid("-1");
			mf.lobject.setOwner(mf.userid);
			mf.lobject.setDesc(mf.problemCtrl.lobjectDescribtion.getText());
			mf.lobject.setTemplate(Integer.toString(mf.templateID));
			mf.lobject.setSolution(sAnswer);
			mf.lobject.setConfig(getParaConfig());

			mf.automata.setId("-1");
			mf.automata.setSource("-1");
			// automata.setName("");

			mf.automata.setGraphStr( mf.ae.getGraphAsString() );
		} else
		{
			mf.lobject.setTitle(mf.problemCtrl.lobjectTitle.getText().trim());
			mf.lobject.setDesc(mf.problemCtrl.lobjectDescribtion.getText());
			mf.lobject.setTemplate(Integer.toString(mf.templateID));
			mf.lobject.setSolution(sAnswer);
			mf.lobject.setConfig(getParaConfig());

			mf.automata.setGraphStr( mf.ae.getGraphAsString() );
		}
		return true;
	}
	
	public Frame getWindowForComponent(Component parentComponent)
	{
		if (parentComponent instanceof Frame)
			return (Frame) parentComponent;
		return getWindowForComponent(parentComponent.getParent());
	}
	
	public MovableEditor getMovableEditor()
	{
		MovableEditor meTemp = new MovableEditor();
		return meTemp;
	}
}
